/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package amd.core.extraction;

/**
 *
 * @author Presario
 */
public class JudulExtractor {

    private static final String ABSTRACT_TITLE = "abstract";

    public static String extract(String stringInput){
        String judulString = "";
        String abstractString = "";

        //cari posisi huruf pertama abstrak dokumen
        String fiftyFirstRowString = getFiftyFirstRowString(stringInput);

        //jika mengandung kata "abstract" pada 50 baris pertama dokumen, ambil abstraknya
        if(isContainAbstractTitle(fiftyFirstRowString)){
            int ljudulPosition = getLastJudulPosition(fiftyFirstRowString);
            judulString=fiftyFirstRowString.substring(0, ljudulPosition);
            System.out.println("JUDUL ="+judulString);
        }
        return judulString;
    }

    private static int getLastJudulPosition(String stringInput){
        return (stringInput.toLowerCase().indexOf(ABSTRACT_TITLE));
    }

    private static String getFiftyFirstRowString(String stringInput) {
        stringInput = stringInput.trim();
        String fiftyFirstRowString = "";
        int iRow = 0;
        int iChar = 0;
        int stringLength = stringInput.length();
        while(iRow<50 && iChar<stringLength){
            char cTemp = stringInput.charAt(iChar);
            if(isRowSeparator(cTemp)){
                iRow++;
            }
            fiftyFirstRowString = fiftyFirstRowString + cTemp;
            iChar++;
        }
        return fiftyFirstRowString;
    }


    private static boolean isContainAbstractTitle(String stringInput) {
        return stringInput.toLowerCase().contains(ABSTRACT_TITLE.toLowerCase());
    }

    private static boolean isRowSeparator(char c) {
        return (c=='\n' || c=='\r');
    }

}
